'''
Created on 03/09/2010

@author: lctr30
'''
import pygtk
pygtk.require('2.0')
import gtk
import sys


class Window(gtk.Window):
    
    __TITLE__ = 'Pemba App'
    __DEF_SIZE__ = 250
    KEY_PRESS_EVENT = 'key_press_event'
    
    def __init__(self):
        gtk.Window.__init__(self)
        self.width = Window.__DEF_SIZE__
        self.height = Window.__DEF_SIZE__
        self.win_pos = gtk.WIN_POS_CENTER_ALWAYS
        self.resizable = False
        self.set_title(Window.__TITLE__)
        self.key_event = {}
        self.key_event_params = {}
        
        
    def show(self):

        self.resize(self.width,self.height)
        
        self.connect('delete_event',self.delete_event)
        self.connect('key_press_event',self.key_press_event)

        self.handler_destroy = self.connect('destroy',self.destroy)
        self.set_title(self.title)
        self.set_position(self.win_pos)
        
#        self.table = gtk.Table(2,3)
        
#        self.window.add(self.create_main_box())
        self.show_all()
#        self.set_resizable(self.resizable)
    
    def set_size(self, width, height):
        self.width = width
        self.height = height
        
    def delete_event(self, widget, event, data=None):
        return False
    
    def set_win_pos(self, win_pos):
        self.win_pos = win_pos
        
      
    def set_key_press_event(self, key_name, func, params=None):
        self.key_event[key_name] = func
        self.key_event_params[key_name] = params

    def key_press_event(self, widget, data=None):
        keyname = gtk.gdk.keyval_name(data.keyval) #@UndefinedVariable
        
        if keyname in self.key_event :
            func = self.key_event[keyname]
            func(self.key_event_params[keyname])
        
    def set_escape_destroy(self):
        self.key_event['Escape'] = self.destroy
        self.key_event_params['Escape'] = self, None

    def destroy(self, widget, data=None):
        gtk.main_quit()
        sys.exit(0)
